//
//  LocationsSearchResultsController.swift
//  SwiftWeather
//
//  Created by shen on 2023/8/28.
//

import UIKit
import Combine

final class LocationsSearchResultsController: BaseViewController, ViewModelable {

    typealias ViewModel = LocationsSearchResultsViewModel

    //MARK: Properties

    var viewModel: ViewModel! {
        didSet {
            viewModel.updateResultsPublisher
                .sink { [weak self] in
                    self?.locationsTableView.reloadData()
                }
                .store(in: &cancellables)

            viewModel.coordinatorPublisher
                .sink { [weak self] city in
                    let controller = LocationForecastViewController(city: city)
                    let navController = UINavigationController(rootViewController: controller)
                    navController.modalPresentationStyle = .automatic
                    self?.present(navController, animated: true)
                }
                .store(in: &cancellables)
        }
    }

    private var cancellables = Set<AnyCancellable>()

    // MARK: - Views

    private lazy var locationsTableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.sf.register(cellWithClass: UITableViewCell.self)
        return tableView
    }()

    // MARK: - View Controller Lyfecycle

    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        viewModel.clearResultsSubject.send()
    }

    // MARK: - Methods

    override func setupViews() {
        locationsTableView.dataSource = self
        locationsTableView.delegate = self
        view.addSubview(locationsTableView, useAutoLayout: true)
    }

    override func constraintViews() {
        NSLayoutConstraint.activate([
            locationsTableView.topAnchor.constraint(equalTo: view.topAnchor),
            locationsTableView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor),
            locationsTableView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor),
            locationsTableView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
        ])
    }
}

// MARK: - UITableViewDataSource & UITableViewDelegate

extension LocationsSearchResultsController: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        viewModel.numberOfResults
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.sf.dequeueReusableCell(withClass: UITableViewCell.self)

        var configuration = cell.defaultContentConfiguration()
        configuration.text = viewModel.titleForLocation(at: indexPath)
        cell.contentConfiguration = configuration

        cell.backgroundColor = Resources.Colors.background
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        viewModel.locationSelectionSubject.send(indexPath)
    }
}
